<template>
  <div>
    <el-form :model="addData" ref="addform" :rules="rules" label-width="120px" size="small">
      <el-form-item label="登录账号">
        <el-input v-model="addData.username" placeholder="请输入" style="width: 80%"/>
      </el-form-item>
      <el-form-item label="姓名">
        <el-input v-model="addData.real_name" placeholder="请输入" style="width: 80%"/>
      </el-form-item>
      <el-form-item label="密码">
        <el-input v-model="addData.password" show-password placeholder="请输入" style="width: 80%"/>
      </el-form-item>
      <el-form-item label="权限组">
        <el-select v-model="addData.group_id" placeholder="请选择" style="width: 80%">
          <el-option
            v-for="item in roleList"
            :key="item.id"
            :label="item.title"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="简单说明">
        <el-input v-model="addData.intro" placeholder="请输入" style="width: 80%"/>
      </el-form-item>
    </el-form>

    <div style="text-align:center">
      <el-button type="primary" size="small" style="padding:8px 30px;" @click="submit">提 交</el-button>
    </div>
  </div>
</template>

<script>
import { postEmp, putEmp, getRoleList } from '@/api/employee'

export default {
  props: {
    addData: {
      type: Object,
      required: true
    },
    isAdd: {
      type: Boolean,
      required: true
    }
  },
  data() {
    return {
      roleList: [],
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        real_name: [
          { required: true, message: '请输入姓名', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ]
      }
    }
  },
  mounted() {
    this.getRoleList();
  },
  methods: {
    /**
     * 获取权限组
     */
    getRoleList() {
      getRoleList({key: this.$store.state.app.activeApp.saa_key,status: '1',is_kefu: '0'}).then(res => {
        if(res.status === 200) {
          this.roleList = res.data;
        }else if(res.status === 204) {
          this.$message.warning('未创建权限角色，请先创建权限角色组');
        }else{
          this.$message.error(res.message);
        }
      })
    },
    /**
     * 提交
     */
    submit() {
      this.$refs.addform.validate((valid) => {
        if(valid) {
          if(this.isAdd){
            postEmp(this.addData).then(response => {
              if(response.status === 200){
                this.$emit('success');
                this.$message.success('添加成功！');
              }else{
                this.$message.error(response.message);
              }
            })
          }else{
            const params = {
              key: this.$store.state.app.activeApp.saa_key,
              username: this.addData.username,
              real_name: this.addData.real_name,
              password: this.addData.password,
              intro: this.addData.intro,
              group_id: this.addData.group_id,
              type: this.addData.type,
              status: this.addData.status,
              id: this.addData.id,
            };
            putEmp(params).then(response => {
              if(response.status === 200){
                this.$emit('success');
                this.$message.success('修改成功！');
              }else{
                this.$message.error(response.message);
              }
            })
          }
        }
      })    
    },
  },
}
</script>